﻿using CommonEngine;
using ManageDto;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using WebAdmin.Models;

namespace WebAdmin.Handler
{
    /// <summary>
    /// 页面业务操作
    /// </summary>
    public sealed class BizPage
    {
        /// <summary>
        /// 检查是否是Ajax请求
        /// </summary>
        public static bool IsAjaxRequest(HttpRequest request)
        {
            if (request == null)
                throw new ArgumentNullException("request");

            if (request.Headers != null)
                return request.Headers["X-Requested-With"] == "XMLHttpRequest";

            return false;
        }

        /// <summary>
        /// 公司树
        /// </summary>
        public static List<jstreeNode> GetCompanyTree(List<CompanyDto> tableData)
        {
            var idSelector = FixedPointFactory.Fix<CompanyDto, string>(f => (org) =>
            {
                return org.CompanyId;
            });
            var parentIdSelector = FixedPointFactory.Fix<CompanyDto, string>(f => (org) =>
            {
                return org.ParentId.IsEmpty() ? "" : org.ParentId;
            });
            var Maping = FixedPointFactory.Fix<CompanyDto, jstreeNode>(f => (node) =>
            {
                jstreeNode newNode = new jstreeNode();
                newNode.id = node.CompanyId;
                newNode.text = node.FullName;
                newNode.state.opened = true;
                return newNode;
            });
            //上级ID是字符串类型，默认值是null，null不参与算法，所以，根节点的默认值为""
            return tableData.GenerateTree(idSelector, parentIdSelector, Maping, x => x.children, "").ToList();
        }

        /// <summary>
        /// 角色授权
        /// </summary>
        public static List<jstreeNode> GetRoleMTree(List<TreeBox> tableData)
        {
            var Maping = FixedPointFactory.Fix<TreeBox, jstreeNode>(f => (node) =>
            {
                jstreeNode newNode = new jstreeNode();
                newNode.id = node.Value;
                newNode.text = node.Name;
                newNode.icon = node.Icon;
                newNode.state.selected = node.HasValue;
                newNode.state.opened = true;
                return newNode;
            });

            List<jstreeNode> nodeLs = new List<jstreeNode>();
            var loop = FixedPointFactory.Fix<List<TreeBox>, List<jstreeNode>>(f => (Ls, pools) =>
            {
                if (Ls == null || Ls.Count < 1)
                    return;

                if (pools == null)
                    pools = new List<jstreeNode>();

                foreach (var box in Ls)
                {
                    var node = Maping(box);
                    pools.Add(node);

                    f(box.Children, node.children);
                }
            });

            loop(tableData, nodeLs);
            return nodeLs;
        }

        /// <summary>
        /// 组织架构
        /// </summary>
        public static List<jstreeNode> GetOrgTree(List<OrgDto> tableData)
        {
            var idSelector = FixedPointFactory.Fix<OrgDto, string>(f => (org) =>
            {
                return org.NodeId;
            });
            var parentIdSelector = FixedPointFactory.Fix<OrgDto, string>(f => (org) =>
            {
                return org.ParentId.IsEmpty() ? "" : org.ParentId;
            });
            var Maping = FixedPointFactory.Fix<OrgDto, jstreeNode>(f => (node) =>
            {
                jstreeNode newNode = new jstreeNode();
                newNode.id = node.NodeId;
                newNode.text = node.NodeName;
                newNode.icon = node.Icon;
                newNode.state.opened = true;
                return newNode;
            });
            //上级ID是字符串类型，默认值是null，null不参与算法，所以，根节点的默认值为""
            return tableData.GenerateTree(idSelector, parentIdSelector, Maping, x => x.children, "").ToList();
        }
    }
}
